///<reference path='../../typings/tsd.d.ts'/>
import express = require("express");
import { ControllerResponse, Controller } from "../base/Controller";
import bodyParser = require("body-parser");
import Sequelize = require("sequelize");
import lightwallet = require("eth-lightwallet");
import HookedWeb3Provider = require("hooked-web3-provider");
import isJSON = require('is-json');
import Web3Util from "../utils/Web3Util";
const Tx = require("ethereumjs-tx");
import Config from "../config/Config";
import LogUtil from "../utils/LogUtil";
import Ipfs from "../lib/Ipfs";

export default class DataController extends Controller {
    protected keystore = null;
    protected pwDerivedKey: Uint8Array = null;


    constructor(keystore,pwDerivedKey: Uint8Array){
        super();

        if (typeof keystore === "string") {
            this.keystore = lightwallet.keystore.deserialize(keystore);
        } else {
            this.keystore = keystore;
        }
        this.pwDerivedKey = pwDerivedKey;
    }

    /**
     * 存储数据
     * @param string data 任意数据
     * @param Function callback
     * @return string hash ipfs上对应的hash 
     */
    public storeData(data:string,callback:Function) {

        Ipfs.store(data,function(files){
            callback(files);
        });

    }

    /**
     * 存储加密数据
     * @param string data json数据
     * @param Function callback
     * @return string hash ipfs上对应的hash 
     */
    public storeCiphereData(data:string,callback:Function) {
        if(this.checkParam(data)){
            let accountAddress = this.getAddress(this.keystore,this.pwDerivedKey);
            let privateKey = this.getPrivateKey(accountAddress, this.keystore, this.pwDerivedKey);
            // let publicKey = this.getPublicKey(this.keystore);
            // console.log('publicKey'+publicKey);
            // console.log('privateKey'+privateKey);

            let th = this;
            //加密数据
            Ipfs.cipher(privateKey,data,function(encrypted){
                //加密数据，存储到ipfs中
                Ipfs.decipher(privateKey,encrypted,function(decrypted){
                    Ipfs.store(encrypted,function(files){
                        if(files.length == 1){
                            files = files[0];
                        }
                        callback(th.responseConstruct(files));
                    });
                });
            });
        }else{
            this.addErrorReason('Data need store is not a json.');
            callback(this.responseConstruct(null));
        }
    }

    /**
     * 读取数据
     * @param string hash ipfs hash
     * @param Function callback 回调函数
     * @param 
     */
    public getData(hash: string,callback: Function){
        var th = this;
        Ipfs.get(hash,function(data){
            callback(th.responseConstruct(data));
        });
    }

    /**
     * 读取解密数据
     * @param string hash ipfs hash
     * @param Function callback 回调函数
     * @param 
     */
    public getDecipcherData(hash: string,callback: Function){
        let accountAddress = this.getAddress(this.keystore,this.pwDerivedKey);
        let privateKey = this.getPrivateKey(accountAddress, this.keystore, this.pwDerivedKey);
        let th = this;
        Ipfs.get(hash,function(data){
            Ipfs.decipher(privateKey,data,function(decrypted){
                //返回解密后的数据
                console.log('decipher data:',decrypted);
                callback(th.responseConstruct(decrypted));
            });
        });
    }

    /**
     * 参数校验
     * @param string data 验证数据
     * @return boolean result 验证结果     */
    private checkParam(data:string){
        return isJSON(data);
    }
}